package com.gitee.ice1938.module.admin.app.manage.appversiondetail;

import java.io.IOException;
import java.security.Principal;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.gitee.ice1938.module.admin.filemanager.fun.FileManageSvc;
import com.gitee.ice1938.module.admin.filemanager.fun.FileNode;
import com.gitee.ice1938.module.admin.filemanager.util.DownloadUtils;
import com.gitee.ice1938.mvc.base.AjaxResult;

/**
 * AppVersionDetailController
 * 
 * 
 * @author ice1938@qq.com 2019-11-29 14:21:39
 *
 */
@Controller
@RequestMapping("/appversiondetail")
public class AppVersionDetailController {

	@Autowired
	private AppVersionDetailService appVersionDetailService;
	@Autowired
	private FileManageSvc fileManageSvc;

	@RequestMapping(path = "/info/{apk_id}", method = RequestMethod.GET)
	public String info(@PathVariable(value = "apk_id") Integer id,HttpServletRequest request, HttpServletResponse response, Model model) {
		//TODO FIXME
		return "module/front/appversiondetail/info";
	}
	/**
	 * 页面索引
	 * 
	 * @param request
	 *            请求
	 * @param response
	 *            响应
	 * @param model
	 *            Model
	 * @return 页面
	 */
	@RequestMapping(method = RequestMethod.GET)
	public String index(HttpServletRequest request, HttpServletResponse response, Model model) {
		if (request.getParameter("package") == null) {
			throw new RuntimeException("package is null");
		}
		return "module/admin/appversiondetail/view_list";
	}

	/**
	 * 插入数据方法
	 * 
	 * @param body
	 *            页面json 数据
	 * @return json 数据
	 * @throws IOException 
	 */
	@RequestMapping(path = "/insert", method = RequestMethod.POST)
	@ResponseBody
	public AjaxResult insert(MultipartFile file, HttpServletRequest request, Principal principal) throws IOException {
		String userLoginName = "anonymous";
		if (principal != null) {
			userLoginName = principal.getName();
		}
		long fileLogastModified = System.currentTimeMillis();//file.getResource().lastModified();
		int flag = appVersionDetailService.insertAppVersionDetail(file.getBytes(),fileLogastModified, userLoginName);
		AjaxResult result = new AjaxResult("添加成功!", 200);
		if (flag == 0) {
			result = new AjaxResult("未添加数据!", 200);
		}
		result.setData(flag);
		return result;
	}
	
	@RequestMapping(value = "/download", method = RequestMethod.GET)
	@ResponseBody
	public void downFile(@RequestParam(value = "appVersionKey") Integer id,HttpServletRequest request, HttpServletResponse response) {
		AppVersionDetailEntity data =appVersionDetailService.findOne(id);
		FileNode fileInfo = fileManageSvc.readFile(data.getAppPath());
		DownloadUtils.download(fileInfo, request, response);
	}

}
